package com.tanhua.manage.listener;
import com.alibaba.fastjson.JSON;
import com.tanhua.domain.vo.FreeZe;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import java.time.Duration;

@Slf4j
@Component
public class UpdateRedisListener extends KeyUpdateEventMessageListener {

    @Autowired
    private  RedisTemplate<String,String> redisTemplate;




    public UpdateRedisListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }



    @Override
    protected void doHandleMessage(Message message) {

        String expiredKey = message.toString();
        String tokenkey=null;
      if("TOKEN".equals(expiredKey.split("_")[0])){
          tokenkey =expiredKey;
          redisTemplate.opsForValue().set("tokenkey",tokenkey, Duration.ofDays(1));
      }
      if(tokenkey!=null){
          log.info("=====tokenkey=========" +tokenkey);
      }

        String key = expiredKey.split("_")[0];
        if("FREEZE".equals(key)&&redisTemplate.hasKey(expiredKey)){
            String freezeJson = redisTemplate.opsForValue().get(expiredKey);
            FreeZe freeZe = JSON.parseObject(freezeJson, FreeZe.class);


            if(freeZe.getFreezingRange()==1){
                String tokenFreeZekey = redisTemplate.opsForValue().get("tokenkey");
                redisTemplate.delete("tokenkey");
                redisTemplate.delete(tokenFreeZekey);

            }
        }
    }

}
